﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SerbinAV.MobileSales.Sales.Common;
using SerbinAV.MobileSales.Sales.Entities;
using SerbinAV.MobileSales.Sales.Views;

namespace SerbinAV.MobileSales.Sales.Presenters
{
    public class MainFormPresenter:IPresenter
    {
        private IWork _work;
        private IMainView _view;

        public MainFormPresenter(IWork work, IMainView view)
        {
            _work = work;
            _view = view;
            
        }
        
        public bool Go()
        {
            initializeForm();
            _view.Show();
            return true;
        }

        private void initializeForm()
        {
            _view.ChangeDataWork += ChangeDataWork;
            _view.SelectCheckVisitAction += CheckVisit;

          //  _view.SelectCreateOrderAction += CreateOrder;
           // _view.EditOrderAction += EditOrder;
           // _view.DeleteOrderAction += DeleteOrder;
           // _view.DetailsOrderAction += DetailsOrder;

            //_view.SelectGrapVisitAction += GrapicsVisit;
            _view.SelectSettingsAction += ShowSettings;
            _view.SelectBuildVisitAction += DailyVisits;

            //_view.SelectImportDataAction += ImportData;
            //_view.SelectExportDataAction += ExportData;

            //_view.SelectAboutAction += AboutAction;
            _view.DateWork = DateTime.Now.Date;

        }

        public void DailyVisits(object sender, EventArgs e)
        {
            _work.PresenterFactory.GetDailyVisitPresenter(_view.DateWork).Go();
            ChangeDataWork(null, null);
        }

        public void ShowSettings(object sender, EventArgs e)
        {
            _work.PresenterFactory.GetSettingsPresenter().Go();
        }

        //public void GrapicsVisit (object sender, EventArgs e)
        //{
        //    new SheduleVisitsForm().Show();
        //}

        public void ChangeDataWork(object sender, EventArgs e)
        {
            try
            {
                _view.DataSource = _work.DataManager.Visits.GetVisitsByDate(_view.DateWork);
            }
            catch (Exception ex)
            {
                _work.HandlerException.Go("Помилка при завантаженні візитів","Помилка",ex);  
            }
        }

        public void CheckVisit(object sender, EventArgs e)
        {
            try
            {
                Visit currentVisit = ((Visit)_view.Current);
                //if (_work.GetSettings().EnableGPS)
                //{
                    //currentVisit.GpsPosition = GpsWorker.GetInstance().GpsData.ToString();
                //}
                currentVisit.TimeEndVisit = DateTime.Now;
                _work.DataManager.Visits.UpdateVisit(currentVisit);
                
            }
            catch (Exception ex)
            {
                _work.HandlerException.Go("Помилка при відмічанні ТТ", "Помилка",ex);
            }
        }

        //public void CreateOrder (object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Visit visit = ((Visit) _view.Current);
        //        if (visit.TimeEndVisit == string.Empty)
        //        {
        //            if (MessageBox.Show("Відмітити візит ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        //            {
        //                CheckVisit(null, null);
        //            }
        //        }
        //        Order tempOrder = new Order();
        //        tempOrder.DateOrder = DateTime.Now.Date;
        //        tempOrder.NotDelivery = Settings.GetInstance().IsVanSales;
        //        string number = string.Empty;
        //        if (Settings.GetInstance().AutoGenerateNumber)
        //        {
        //            Settings.GetInstance().NumberDocument++;
        //            number = Settings.GetInstance().NumberDocument.ToString();
        //        }
        //        tempOrder.NumberOrder = number;
        //        tempOrder.SkladId = Settings.GetInstance().DefaultSklad;
        //        tempOrder.VisitId = visit.IdVisit;
        //        tempOrder.IsReadOnly = false;

        //        new OrderForm(
        //            DataManager.GetInstance().Orders.CreateOrder(tempOrder), visit.BuyPoint, true).ShowDialog();
        //        ChangeDataWork(null, null);
        //    }
        //    catch (Exception ex)
        //    {
        //        ExceptionWork.ExeptionProcess(ex);
        //    }
        //}

        //public void EditOrder (object sender, EventArgs e)
        //{
        //    Visit visit = ((Visit)_view.Current);
        //    new OrderForm(_view.CurrentOrder, visit.BuyPoint, true).ShowDialog();
        //    ChangeDataWork(null, null);
        //}

        //public void DeleteOrder (object sender, EventArgs e)
        //{
        //    DataManager.GetInstance().Orders.DeleteOrder((_view.CurrentOrder).IdOrder);
        //    ChangeDataWork(null, null);
        //}

        //public void DetailsOrder(object sender, EventArgs e)
        //{
        //    Visit visit = ((Visit)_view.Current);
        //    new OrderForm(_view.CurrentOrder, visit.BuyPoint, false).ShowDialog();
        //    ChangeDataWork(null, null);
        //}

        //public void ImportData (object sender, EventArgs e)
        //{
        //    new ImportForm(true).ShowDialog();
        //    ChangeDataWork(null, null);
        //}

        //public void ExportData (object sender, EventArgs e)
        //{
        //    new ImportForm(false).ShowDialog();
        //    ChangeDataWork(null, null);
        //}

        //public void AboutAction (object sender, EventArgs e)
        //{
        //    new AboutForm().ShowDialog();
        //}




    }
}
